<?php
if ( IPB_THIS_SCRIPT != 'admin' )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'init.php'.";
	exit();
}

class ad_cache
{
	var $ipsclass;
	var $html;
	var $map;
	var $call;
	
	function auto_run()
	{
		//-----------------------------------------
		// Require and RUN !! THERES A BOMB
		//-----------------------------------------
		
		$this->ipsclass->admin->page_detail = "The cache manager allows you to view the contents of your cache and update them.";
		$this->ipsclass->admin->page_title  = "Cache Manager";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=cache', 'Cache Manager' );
		
		//-----------------------------------------
		// Load skin
		//-----------------------------------------
		
		$this->html = $this->ipsclass->acp_load_template('cp_skin_tools');
		
		//-----------------------------------------
		// Kill globals - globals bad, Homer good.
		//-----------------------------------------
		
		$tmp_in = array_merge( $_GET, $_POST, $_COOKIE );
		
		foreach ( array_keys($tmp_in) as $k )
		{
			unset($$k);
		}
		
		//-----------------------------------------
		// Map
		//-----------------------------------------
		
		$this->map = array(
							'forum_cache'     => 'All forum information and data',
							'group_cache'     => 'All member group infomation and data',
							'market_category' => 'ibMarket category cache',
							'market_items'    => 'ibMarket items cache',  
							'market_plugin'   => 'ibMarket plugin cache',
							'market_wages'    => 'ibMarket regular wages cache',
							'market_taxes'    => 'ibMarket regular taxes cache',
						  );
		$this->call = array(
							'forum_cache'     => array(&$this->ipsclass,'update_forum_cache'),
							'group_cache'     => array(&$this->ipsclass->market_class,'group_cache'),
							'market_category' => array(&$this->ipsclass->market_class,'cache_categories'),
							'market_items'    => array(&$this->ipsclass->market_class,'cache_items'),  
							'market_plugin'   => array(&$this->ipsclass->market_class,'cache_plugins'),
							'market_wages'    => array(&$this->ipsclass->market_class,'cache_wages'),
							'market_taxes'    => array(&$this->ipsclass->market_class,'cache_taxes'),
						  );
		
		//-----------------------------------------
		// Load in the plugins
		//-----------------------------------------
		
		if(is_array($this->ipsclass->cache['market_plugin']) && count($this->ipsclass->cache['market_plugin']))
		{
			foreach($this->ipsclass->cache['market_plugin'] as $plugin_info)
			{
				$plugin = $this->ipsclass->market_class->load_plugin($plugin_info);
				
				foreach($plugin->cache_array as $key => $value)
				{
					$this->map[$key] = $value[0];
					$this->call[$key] = array($plugin,$value[1]);
				}
			}
		}
		
		//-----------------------------------------
		// What to do...
		//-----------------------------------------
		
		switch($this->ipsclass->input['code'])
		{
			case 'cacheend':
				$this->cache_end();
				break;
				
			case 'viewcache':
				$this->view_cache();
				break;
			
			case 'cache_update_all':
				$this->ipsclass->admin->output_multiple_redirect_init( $this->ipsclass->base_url.'&'.$this->ipsclass->form_code_js.'&code=cache_update_all_process&id=0' );
				break;
				
			case 'cache_update_all_process':
				$this->cache_update_all_process();
				break;
		
			default:
				$this->cache_start();
				break;
		}
	}
	
	/*-------------------------------------------------------------------------*/
	// Process....
	/*-------------------------------------------------------------------------*/
	
	function cache_update_all_process()
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		
		$id         = intval( $this->ipsclass->input['id'] );
		$cache_name = '';
		$count      = 0;
		$img        = '<img src="'.$this->ipsclass->skin_acp_url.'/images/aff_tick_small.png" border="0" alt="-" /> ';
		
		//-----------------------------------------
		// Get cache name
		//-----------------------------------------
		
		foreach( array_keys($this->map) as $name )
		{
			if ( $count == $id )
			{
				$cache_name = $name;
				break;
			}
			
			$count++;
		}
		
		//-----------------------------------------
		// Do what, now?
		//-----------------------------------------
		
		$id++;
		
		if ( $cache_name )
		{
			$this->cache_end( $cache_name, 1 );
			
			$this->ipsclass->admin->output_multiple_redirect_hit( $this->ipsclass->base_url.'&'.$this->ipsclass->form_code_js.'&code=cache_update_all_process&id='.$id, $img.$cache_name.' processed...' );
		}
		else
		{
			$this->ipsclass->admin->output_multiple_redirect_done();
		}
	}
	
	/*-------------------------------------------------------------------------*/
	// DO VIEW
	/*-------------------------------------------------------------------------*/
	
	function view_cache()
	{
		if ( ! $this->ipsclass->input['cache'] )
		{
			$this->ipsclass->main_msg = "No ID was passed, please try again";
			$this->cache_start();
		}
		
		$row = $this->ipsclass->DB->simple_exec_query( array( 'select' => '*', 'from' => 'cache_store', 'where' => "cs_key='{$this->ipsclass->input['cache']}'" ) );
		
		ob_start();
		if ( $row['cs_array'] )
		{
			print_r( unserialize($this->ipsclass->txt_stripslashes($row['cs_value'])) );
		}
		else
		{
			print $row['cs_value'];
		}
		
		$out = ob_get_contents();
		ob_end_clean();
		
		$this->ipsclass->html = "<pre>".htmlspecialchars($out)."</pre>";
		
		$this->ipsclass->admin->print_popup();
	}
		
	/*-------------------------------------------------------------------------*/
	// DO UPDATE
	/*-------------------------------------------------------------------------*/
	
	function cache_end( $cache_name='', $dontcancel=0 )
	{
		if ( ! $cache_name )
		{
			$cache_name = $this->ipsclass->input['cache'];
		}
		
		if( isset($this->call[ $cache_name ]) )
		{
			call_user_func($this->call[ $cache_name ]);
			$this->ipsclass->main_msg = 'Cache updated';
		}
		
		if ( ! $dontcancel )
		{
			$this->cache_start();
		}
	}
	
	/*-------------------------------------------------------------------------*/
	// SHOW CACHE FORM
	/*-------------------------------------------------------------------------*/
	
	function cache_start()
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		
		$used    = array();
		$content = "";
		
		//-----------------------------------------
		// SHOW CACHES
		//-----------------------------------------
		
		$this->ipsclass->DB->simple_construct( array( 'select' => '*', 'from' => 'cache_store', 'order' => 'cs_key' ) );
		$this->ipsclass->DB->simple_exec();
		
		while ( $row = $this->ipsclass->DB->fetch_row() )
		{
			if ( ! in_array( $row['cs_key'], array_keys( $this->map ) ) )
			{
				continue;
			}
			
			$used[ $row['cs_key'] ] = $row['cs_key'];
			$row['_size']           = ceil( intval( strlen( $row['cs_value'] ) ) / 1024 );
			$row['_desc']           = $this->map[ $row['cs_key'] ];
			
			$content .= $this->html->cache_row( $row );
		}
		
		if ( count( $used ) != count( $this->map ) )
		{
			foreach( $this->map as $k => $v )
			{
				if ( in_array( $k, array_keys( $used ) ) )
				{
					continue;
				}
				else
				{
					$row['cs_key']   = $k;
					$row['cs_value'] = $v;
					$row['_size']    = 0;
					$row['_desc']    = $this->map[ $row['cs_key'] ];
					
					$content .= $this->html->cache_row( $row );
				}
			}
		}
		
		$this->ipsclass->html .= $this->html->cache_overview( $content );
		
		$this->ipsclass->admin->output();
	}
	
	/*-------------------------------------------------------------------------*/
	// PERL
	/*-------------------------------------------------------------------------*/
	
	function perly_length_sort($a, $b)
	{
		if ( strlen($a['typed']) == strlen($b['typed']) )
		{
			return 0;
		}
		return ( strlen($a['typed']) > strlen($b['typed']) ) ? -1 : 1;
	}
	
	function perly_word_sort($a, $b)
	{
		if ( strlen($a['type']) == strlen($b['type']) )
		{
			return 0;
		}
		return ( strlen($a['type']) > strlen($b['type']) ) ? -1 : 1;
	}
}
?>